import os.path

from bs4 import BeautifulSoup
from fake_useragent import UserAgent
import requests


class BaiduHotMovie(object):
    def __init__(self):
        self.base_dir = os.path.dirname(__file__)
        self.page_dir = os.path.join(self.base_dir, "page")
        self.headers = {
            "User-Agent": UserAgent().random
        }

    def save_page(self, data, file_name="baidu_hot_movie"):
        os.makedirs(self.page_dir, exist_ok=True)
        file_path = os.path.join(self.page_dir, file_name + '.html')
        with open(file_path, "w", encoding="utf-8") as f:
            f.write(data)

    def read(self, file_name="baidu_hot_movie"):
        os.makedirs(self.page_dir, exist_ok=True)
        file_path = os.path.join(self.page_dir, file_name + '.html')
        with open(file_path, "r", encoding="utf-8") as f:
            data = f.read()
        return data

    def spider_baidu_hot_movie(self):
        target_url = "https://top.baidu.com/board?tab=movie"
        res = requests.get(target_url, headers=self.headers)
        self.save_page(res.text)

    def parse_result(self):
        data = self.read()
        soup = BeautifulSoup(data, "lxml")
        parse_res_all = {}
        div_list = soup.select("#sanRoot > main > div.container.right-container_2EFJr > div > div:nth-child(2) > div")
        for div in div_list:
            # 封面
            movie_img_url = div.select("a.img-wrapper_29V76 > img")[0].attrs.get("src")
            # 标题
            movie_title = div.select(".c-single-text-ellipsis")[0].text.strip()
            # 类型
            movie_type = div.select("div > div:nth-child(2)")[0].text.split("：")[1].split("、")
            # 演员
            movie_actors = div.select("div > div:nth-child(3)")[0].text.split("：")[1]
            # 描述
            movie_description = div.select("div > div.c-single-text-ellipsis.desc_3CTjT")[0].text.replace("查看更多>  ",
                                                                                                          "")
            # 热搜指数
            movie_hot_num = div.select("a.trend_2RttY > div.hot-index_1Bl1a")[0].text.strip()
            parse = {
                "movie_img_url": movie_img_url,
                "movie_title": movie_title,
                "movie_type": movie_type,
                "movie_actors": movie_actors,
                "movie_description": movie_description,
                "movie_hot_num": movie_hot_num
            }
            parse_res_all[movie_title] = parse
        return parse_res_all


if __name__ == '__main__':
    bhm = baidu_hot_movie()
    # bhm.spider_baidu_hot_movie()
    parse_data = bhm.parse_result()
    print(parse_data)
